package com.jht.bletool2java.characteristic.userdata;


import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import androidx.annotation.Keep;


import com.jht.bletool2java.characteristic.TranslateData;
import com.jht.bletool2java.util.ByteUtil;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid = "00002a85-0000-1000-8000-00805f9b34fb")
public class DateofBirth implements TranslateData {

    private static final String TAG = "DateofBirth";
    private String year = "";
    private String month = "";
    private String day = "";
    private byte[] value;
    private boolean isInvalidData = false;
    public DateofBirth() {
    }
    public DateofBirth(BluetoothGattCharacteristic characteristic) {
        if (characteristic == null){
            Log.w(TAG, "Date of Birth: characteristic is null!");
            isInvalidData = true;
            return;
        }
        value = characteristic.getValue();
        isInvalidData = (value == null || value.length < 4);
        if (!isInvalidData){
            int i = ByteUtil.bytes2ToInt(value,0);
            year += i;
            if (i == 0){
                year =  "Year is not known";
            }
            int i2 = ByteUtil.byte1ToInt(value,2);
            switch (i2){
                case 1:
                    month += "January";
                    break;
                case 2:
                    month += "February";
                    break;
                case 3:
                    month += "March";
                    break;
                case 4:
                    month += "April";
                    break;
                case 5:
                    month += "May";
                    break;
                case 6:
                    month += "June";
                    break;
                case 7:
                    month += "July";
                    break;
                case 8:
                    month += "August";
                    break;
                case 9:
                    month += "September";
                    break;
                case 10:
                    month += "October";
                    break;
                case 11:
                    month += "November";
                    break;
                case 12:
                    month += "December";
                    break;
                case 0:
                default:
                    month  = "Month is not known";
                    break;
            }
            int i3 = ByteUtil.byte1ToInt(value,3);
            day += i3;
            if (i3 <= 0 || i3 > 31){
                day = "Day of Month is not known";
            }
        }

    }

    public DateofBirth(byte[] value) {
        this.value = value;
        isInvalidData = (value == null || value.length < 4);
        if (!isInvalidData){
            int i = ByteUtil.bytes2ToInt(value,0);
            year += i;
            if (i == 0){
                year =  "Year is not known";
            }
            int i2 = ByteUtil.byte1ToInt(value,2);
            switch (i2){
                case 1:
                    month += "January";
                    break;
                case 2:
                    month += "February";
                    break;
                case 3:
                    month += "March";
                    break;
                case 4:
                    month += "April";
                    break;
                case 5:
                    month += "May";
                    break;
                case 6:
                    month += "June";
                    break;
                case 7:
                    month += "July";
                    break;
                case 8:
                    month += "August";
                    break;
                case 9:
                    month += "September";
                    break;
                case 10:
                    month += "October";
                    break;
                case 11:
                    month += "November";
                    break;
                case 12:
                    month += "December";
                    break;
                case 0:
                default:
                    month  = "Month is not known";
                    break;
            }
            int i3 = ByteUtil.byte1ToInt(value,3);
            day += i3;
            if (i3 <= 0 || i3 > 31){
                day = "Day of Month is not known";
            }
        }
    }
    @Override
    public String convert2String() {
        return "Year: " + year +" ; Month: " + month +" ; Day: " + day+" ;"+"\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }
    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {
        return value;
    }


    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {
        isInvalidData = (value == null || value.length < 4);
        if (isInvalidData){
            //非法数据
            this.value =value;
            year = "";
            month = "";
            day = "";
            Log.w(TAG, "parseData: value == null || value.length != 4");
        }else {
            this.value = value;
            int i = ByteUtil.bytes2ToInt(value,0);
            year  = ""+i;
            if (i == 0){
                year =  "Year is not known";
            }
            int i2 = ByteUtil.byte1ToInt(value,2);
            switch (i2){
                case 1:
                    month  = "January";
                    break;
                case 2:
                    month  = "February";
                    break;
                case 3:
                    month  = "March";
                    break;
                case 4:
                    month  = "April";
                    break;
                case 5:
                    month  = "May";
                    break;
                case 6:
                    month  = "June";
                    break;
                case 7:
                    month  = "July";
                    break;
                case 8:
                    month  = "August";
                    break;
                case 9:
                    month  = "September";
                    break;
                case 10:
                    month  = "October";
                    break;
                case 11:
                    month  = "November";
                    break;
                case 12:
                    month  = "December";
                    break;
                case 0:
                default:
                    month  = "Month is not known";
                    break;
            }
            int i3 = ByteUtil.byte1ToInt(value,3);
            day  = ""+i3;
            if (i3 == 0 ){
                day = "Day of Month is not known";
            }
        }

    }
}
